#ifndef CLIENT_H
#define CLIENT_H

#include "protocol.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <unistd.h>

#ifndef PORT
#define PORT 8852
#endif

#define MAXMEMBERS 15

#define CL_CMDS 0
/*
 * U - up
 * D - down
 * C - center
 * L - left
 * R - right
 */
#define CL_DL 1
#define CL_D  2
#define CL_DR 3
#define CL_L  4
#define CL_C  5
#define CL_R  6
#define CL_UL 7
#define CL_U  8
#define CL_UR 9
#define CL_SQ_MIN 1
#define CL_SQ_MAX 9

#define CL_USERNAME 10
#define CL_JOIN     11
#define CL_LEAVE    12
/* #define CL_PLAYERS  13 */

#define CHAR_ROW " %c | %c | %c\n"
#define BORDER_ROW "---+---+---\n"
#define GPR(sq) getPlayerRepr((sq))

#define DEFAULT_IP "67.110.193.152"

/* Functions */
int init(char* username, char* ip, int port);
void haltConnection(void);
void startReading(void);
void join(int);
void leave(int);
Player* getPlayer(int);
void displayRoomMembers(int);
void *parseInput(void*);
void sendMove(CODE, int);
int readRoom(void);
int readPlayer(void);
void hastMoves(int);
int checkIfInRoom();
int isInRange(int, int, int);
int isDirection(CODE);
int getPlayerRepr(int);
void displayBoard(void);
void storeMove(int mv);

/* Variables */
typedef struct connection {
	int sockfd;
	int connected;
	pthread_t thread;
	Player* p[MAXMEMBERS];
	Room* rm[MAXMEMBERS];
	int inRoom;
} Connection;

extern Connection server;

#ifdef USE_DEFAULT_IP
extern char defaultIP[];
#endif

#endif
